/**
 * 明镜1.0.0.0 rc 版本视频上传
 */
function main() {
    debugger;
    const self = this;
    let fileName = self.$$("text_input_9nadmq").cValue;
    self.isLoading = true;

    // 校验视频名称
    if (!fileName) {
        self.$message.warning("请填写视频名称！");
        self.isLoading = false;
        return;
    }

    // 获取上传文件
    let inputEle = window.INPUELEMENT;
    if (!inputEle || inputEle == "") {
        self.$message.warning("请上传视频！");
        self.isLoading = false;
        return;
    }

    var file = inputEle.files[0];
    if (!file) {
        self.$message.warning("请上传视频！");
        self.isLoading = false;
        return;
    }

    // 构建 FormData
    var formData = new FormData();
    var indexFlag = "0_" + Date.now();
    var indexFlag1 = "1_" + Date.now();
    formData.append(indexFlag, file);

    var fileExtension = file.name.split('.').pop();
    let fileNameValue = fileName + '.' + fileExtension;
    formData.append(indexFlag1, fileNameValue);

    let formParams = [
        { indexFlag: indexFlag, name: "file", type: "FormData", valueType: "FILE", valueContent: '' },
        { name: "fileName", type: "FormData", valueType: "STRING", valueContent: fileNameValue },
        { name: "name", type: "FormData", valueType: "STRING", valueContent: fileNameValue },
        { name: "size", type: "FormData", valueType: "STRING", valueContent: '10000' },
        { name: "duration", type: "FormData", valueType: "STRING", valueContent: '-1' },
        { name: "tagCode", type: "FormData", valueType: "STRING", valueContent: '-1' },
        { name: "tagName", type: "FormData", valueType: "STRING", valueContent: '-1' },
        { name: "path", type: "FormData", valueType: "STRING", valueContent: '-1' },
        { name: "serverType", type: "FormData", valueType: "STRING", valueContent: '1' },
    ]


    // 调用接口上传
    api.DATA_SOURCE_EXECUTE("741ac3c4-fcd8-4ac1-b1fe-89c29ac4bcdf", "api", [], formParams, "", formData).then(function (res) {
        console.log(res);
        if (res && !res.hasError) {
            self.$message.success("视频上传成功！");
            let treePanel = window.treePanel;
            self.isLoading = false;
            // 执行数据刷新
            treePanel.$$refreshData().then(function () {
                closeMingJingDialog();
            });
        } else {
            self.isLoading = false;
            self.$message.error("视频上传失败，请重试！");
        }
    }).catch(function (err) {
        console.error(err);
        self.isLoading = false;
    });
}

function closeMingJingDialog() {
    if (window) {
        window.postMessage(
            JSON.stringify({
                event: "close-dialog",
                param: { "XXX": "test" }
            }),
            "*"
        );
    }
}
